package com.xuetang9.demo.ssm.web;

import java.util.HashMap;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.xuetang9.demo.ssm.common.ValidationUtis;
import com.xuetang9.demo.ssm.common.request.DataTableRequest;
import com.xuetang9.demo.ssm.common.response.DataTableResponse;
import com.xuetang9.demo.ssm.domain.Resource;
import com.xuetang9.demo.ssm.domain.validation.Insert;
import com.xuetang9.demo.ssm.service.ResourceService;

@Controller
public class ResourceController {

	@Autowired
	private ResourceService resourceService;
	@RequestMapping(path="/system/resource/list")
	public String list() {
		return "resource/list";
	}
	
	@RequestMapping(path="/system/resource.json")
	
	@ResponseBody // 自动把返回数据变成JSON格式
	public Object listJSON(@RequestBody DataTableRequest dataTableRequest) {
		
//		Map<String, Object> json = new HashMap<>();
//		json.put("data",resourceService.listAll());
//		json.put("recordsFiltered", 96);
//		json.put("recordsTotal", 96);
//		json.put("draw", 1);
//		return json ;
		DataTableResponse<Resource> json = new DataTableResponse<>();
		// draw接收到客户端的请求，原样返回
		json.setDraw(dataTableRequest.getDraw());
		
		// 计算当前的页码
		int pageNum = dataTableRequest.getStart() / dataTableRequest.getLength() + 1;
		
		Page<Resource> page = resourceService.listByPage(pageNum,  dataTableRequest.getLength());
		
		// 设置数据总条数
		json.setRecordsTotal(page.getTotal());
		json.setRecordsFiltered(page.getTotal());
		// 设置查询出来的数据
		json.setData(page.getResult());
		return json;
	}
	
	@RequestMapping(path = "/system/resource/new",method=RequestMethod.GET)
	public String create() {
		return "resource/new-resource";
	}
	
//	@RequestMapping(path = "/system/resource/new",method=RequestMethod.POST)
//	@ResponseBody
//	public Map<String,Object> create(Resource resource,Errors errors) {
//		
//		boolean success = true;
//		List<String> validMessage = new ArrayList<>();
//		
//		// 执行后台的表单验证
//		/*if(resource.getName() == null || resource.getName().trim().length() == 0) {
//			success = false;
//			validMessage.add("资源名称必填");
//		}*/
//		
//		if(resourceSaveValidation.supports(resource.getClass())) {
//			resourceSaveValidation.validate(resource, errors);
//		}
//		success = !errors.hasErrors();
//		
//		for (ObjectError error : errors.getAllErrors()) {
//			validMessage.add(error.getDefaultMessage());
//		}
//		
//		
//		Map<String, Object> json = new HashMap<>();
//		json.put("success", success);
//		json.put("message", validMessage);
//		
//		return json;
//	}
	
	@RequestMapping(path = "/system/resource/new",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> create(@Validated(Insert.class) Resource resource, BindingResult bindingResult) {
		
		// 创建一个map用于存放json数据的内容
		Map<String, Object> json = new HashMap<>();
		
		if(bindingResult.hasErrors()) {
			json.put("success", false);
			json.put("message", ValidationUtis.bindResultToList(bindingResult) );
		}else {
			// 调用业务层
			json.put("success", true);
		}
		return json;
		
	}
	
	
}
